package com.assignwork.service.impl;

import com.assignwork.dto.ShipForecastSearchDTO;
import com.assignwork.dto.VoyageInfoSubmitDTO;
import com.assignwork.entity.VoyageInfo;
import com.assignwork.entity.transport.ShipForecast;
import com.assignwork.mapper.VoyageInfoMapper;
import com.assignwork.service.IVoyageInfoService;
import com.assignwork.service.transport.IShipForecastService;
import com.assignwork.vo.ShipVoyagePageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 航次信息实现类
 */
@Service
@AllArgsConstructor
public class VoyageInfoServiceImpl extends BaseServiceImpl<VoyageInfoMapper, VoyageInfo> implements IVoyageInfoService {

    private IShipForecastService shipForecastService;

    @Override
    public IPage<ShipVoyagePageVO> selectVoyagePage(ShipForecastSearchDTO dto, IPage<ShipVoyagePageVO> page) {
        List<ShipVoyagePageVO> data = baseMapper.selectVoyageList(dto, page);
        for (ShipVoyagePageVO vo : data) {
            if (!Func.isNull(vo.getForecastStatus())) {
                vo.setForecastStatusName(DictBizCache.getValue("forecast_status", vo.getForecastStatus()));
            }
            if (!Func.isNull(vo.getBerth())) {
                vo.setBerthName(DictBizCache.getValue("berth", vo.getBerth()));
            }
            if (!Func.isNull(vo.getTradeType())) {
                vo.setTradeTypeName("1".equals(vo.getTradeType())?"外贸":"内贸");
            }

        }
        return page.setRecords(data);
    }

    @Override
    public String submitVoyageInfo(VoyageInfoSubmitDTO dto) {
        if (Func.isNull(dto.getVoyageId())) {
            QueryWrapper<VoyageInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(VoyageInfo::getIsDeleted, 0).eq(VoyageInfo::getShipId, dto.getShipId()).eq(VoyageInfo::getImpExp, dto.getImpExp());
            List<VoyageInfo> voyageInfos = baseMapper.selectList(queryWrapper);
            if (!Func.isNull(voyageInfos) && !voyageInfos.isEmpty()) {
                dto.setVoyageId(voyageInfos.get(0).getId());
            }
        }

        VoyageInfo entity = BeanUtil.copy(dto, VoyageInfo.class);
        entity.setId(dto.getVoyageId());
        super.saveOrUpdate(entity);
        return entity.getId().toString();
    }

    @Override
    public Boolean submitShow(VoyageInfoSubmitDTO dto) {
        if (Func.isNull(dto.getShipId())) {
            throw new RuntimeException("缺少参数：shipId");
        }
        if (Func.isNull(dto.getImpExp())) {
            throw new RuntimeException("缺少参数：impExp");
        }
        if (Func.isNull(dto.getIsShow())) {
            throw new RuntimeException("缺少参数：isShow");
        }
        ShipForecast sf = shipForecastService.getById(dto.getShipId());
        if ("I".equals(dto.getImpExp())) {
            sf.setVoyageImpShow(dto.getIsShow());
        }
        if ("E".equals(dto.getImpExp())) {
            sf.setVoyageExpShow(dto.getIsShow());
        }
        return shipForecastService.saveOrUpdate(sf);
    }
}